from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.views import View
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView


@csrf_exempt
def view0(request):
    # get  --> get on view0
    # post --> post on view0
    desc = '说明：第0个视图函数'
    if request.method == 'GET':
        return HttpResponse(f'get on view0 ({desc})')
    else:
        return HttpResponse(f'post on view0 ({desc})')


@method_decorator(csrf_exempt, name='dispatch')  # 怎么样给类视图添加装饰器（方式二）
class View1(View):
    desc = '说明：第1个视图函数'

    # @method_decorator(csrf_exempt)  # 怎么样给类视图添加装饰器（方式一）
    # def dispatch(self, request, *args, **kwargs):
    #     return super(View1, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return HttpResponse(f'get on view1 ({self.desc})')

    def post(self, request, *args, **kwargs):
        return HttpResponse(f'post on view1 ({self.desc})')


class GetMixin:

    def get(self, request, *args, **kwargs):
        return HttpResponse(f'get on view1 ({self.desc})')


class PostMixin:
    def post(self, request, *args, **kwargs):
        return HttpResponse(f'post on view1 ({self.desc})')


@method_decorator(csrf_exempt, name='dispatch')  # 怎么样给类视图添加装饰器（方式二）
class View2(GetMixin, PostMixin, View):
    desc = '说明：第2个视图函数'


# 混入 Mixin 类 多重继承  组合式编程
# 以下两点为了防混乱
# 1. 在继承的时候混入的类就不要继承View
# 2. 继承的类里面只要有一个来自于View


def about1(request):
    return render(request, 'class/about.html')


class About3(TemplateView):
    template_name = 'class/about.html'